package com.lvyq.myplayer.common.util

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.lvyq.myplayer.R

/**
 * 文件描述：CustomUpdateDialog 版本升级窗口
 * 作者：lvyq
 * 创建时间：2025-05-13
 * 修改时间：2025-05-13
 */
class CustomUpdateDialog(context: Context) : Dialog(context) {

    private var onConfirmListener: (() -> Unit)? = null
    private var onCancelListener: (() -> Unit)? = null

    init {
        setContentView(R.layout.dialog_upversion)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setLayout(
            (context.resources.displayMetrics.widthPixels * 0.85).toInt(),
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        // 设置动画
        window?.setWindowAnimations(R.style.DialogAnimation)

        // 初始化视图
        val tvTitle = findViewById<TextView>(R.id.tvTitle)
        val tvContent = findViewById<TextView>(R.id.tvContent)
        val btnConfirm = findViewById<Button>(R.id.btnConfirm)
        val btnCancel = findViewById<Button>(R.id.btnCancel)

        // 按钮点击
        btnConfirm.setOnClickListener {
            onConfirmListener?.invoke()
            dismiss()
        }

        btnCancel.setOnClickListener {
            onCancelListener?.invoke()
            dismiss()
        }
    }

    // 设置标题
    fun setTitle(title: String): CustomUpdateDialog {
        findViewById<TextView>(R.id.tvTitle).text = title
        return this
    }

    // 设置内容
    fun setContent(content: String): CustomUpdateDialog {
        findViewById<TextView>(R.id.tvContent).text = content
        return this
    }

    // 确认按钮监听
    fun setOnConfirmListener(listener: () -> Unit): CustomUpdateDialog {
        onConfirmListener = listener
        return this
    }

    // 取消按钮监听
    fun setOnCancelListener(listener: () -> Unit): CustomUpdateDialog {
        onCancelListener = listener
        return this
    }

    // 显示对话框
    override fun show() {
        super.show()
        // 防止点击外部关闭
        setCanceledOnTouchOutside(false)
    }





}